package com.heima.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.hash.HashCode;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.PageResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.pojos.WmNews;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * @author mgw
 * @create 2021/9/26 17:36
 */
@Service
@Slf4j
public class ApAtricalServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Override
    public ResponseResult homeList(Integer tag, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Page<ApArticle> articles = apArticleMapper.homeList(tag);
        PageResult<ApArticle> pageResult = new PageResult<ApArticle>();
        pageResult.setPage(page);
        pageResult.setPagesize(size);
        pageResult.setPages(articles.getPages());
        pageResult.setTotal(articles.getTotal());
        pageResult.setResult(articles.getResult());
        return ResponseResult.okResult(pageResult);
    }

    @Override
    public ResponseResult<ApArticleContent> findArticleById(String articleId) {
      ApArticleContent apArticleContent= apArticleMapper.findArticleById(articleId);
        return ResponseResult.okResult(apArticleContent);
    }

    @Override
    public ResponseResult updateStaticUrl( String url, String id) {
        apArticleMapper.updateStaticUrl(url,id);
        return ResponseResult.okResult(null);
    }

    @Override
    public void syncArticle(WmNews wmNews) {
        ApArticle apArticle = new ApArticle();
        apArticle.setId(wmNews.getId().toString());
        apArticle.setTitle(wmNews.getTitle());
        apArticle.setAuthorId(wmNews.getUserId().longValue());
        apArticle.setChannelId(wmNews.getChannelId());
        apArticle.setLayout(wmNews.getType().toString());
        apArticle.setFlag(0);
        apArticle.setImages(wmNews.getImages());
        apArticle.setLabels(wmNews.getLabels());
        apArticle.setIsDelete(0);
        if (wmNews.getPublishTime()==null||wmNews.getPublishTime().getTime()<=System.currentTimeMillis()){
            apArticle.setIsDown(0);
        }else {
            apArticle.setIsDown(1);
        }
        apArticleMapper.insert(apArticle);
        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setArticleId(Long.valueOf(apArticle.getId()));
        apArticleContent.setContent(wmNews.getContent());
        apArticleContentMapper.insert(apArticleContent);


    }

    @Override
    public void downOrUp(Map<String, Object> resultMap) {
        for (Map.Entry<String, Object> stringObjectEntry : resultMap.entrySet()) {
            Object id = stringObjectEntry.getKey();
            String idstr = id.toString();
            Object downUp = stringObjectEntry.getValue();
            String downUpstr = downUp.toString();
            ApArticle apArticle = new ApArticle();
            apArticle.setId(idstr);
            apArticle.setIsDown(Integer.valueOf(downUpstr));
            apArticleMapper.updateById(apArticle);
        }
    }
}
